<?php

namespace backend\controllers;

use backend\models\Equity;
use Yii;
use backend\models\EduEnter;
use backend\models\EduEnterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * EduEnterController implements the CRUD actions for EduEnter model.
 */
class EduEnterController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all EduEnter models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new EduEnterSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single EduEnter model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $equity = $model->equity;
        if ($equity) {
            $equityGet = json_decode($equity);
            $str = "";
            foreach ($equityGet as $item) {
                $str .= $item->equity .'--';
            }
            $model->equity = $str;
        }
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new EduEnter model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new EduEnter();
        $modelsEquity = [new Equity];
        $model->scenario = 'create';
        if ($model->load(Yii::$app->request->post())) {
            $equity = Yii::$app->request->post()['Equity'];
            if ($equity) {
                $model->equity = json_encode($equity);
            }
            $model->create_time = time();
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
            'modelsEquity' => (empty($modelsEquity)) ? [new Equity] : $modelsEquity
        ]);
    }

    /**
     * Updates an existing EduEnter model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $arrayEquity = json_decode($model->equity);
        $modelsEquity = [];
        foreach ($arrayEquity as $item) {
            $next = new Equity;
            $next->equity = $item->equity;
            $modelsEquity[] = $next;
        }
        $model->scenario = 'update';
        if ($model->load(Yii::$app->request->post())) {
            $equity = Yii::$app->request->post()['Equity'];
            if ($equity) {
                $model->equity = json_encode($equity);
            }
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
            'modelsEquity' => $modelsEquity,
        ]);
    }

    /**
     * Deletes an existing EduEnter model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the EduEnter model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return EduEnter the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = EduEnter::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
